package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result list(UserDto userDto) {
        log.info("列表查询: {}",userDto);
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result add(@RequestBody User user){
        log.info("新增用户: {}", user);
        userService.add(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("删除用户: {}", ids);
        userService.deleteByIds(ids);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询单个: {}", id);
        User user = userService.selectById(id);
        return Result.success(user);
    }

    @PutMapping
    public Result update(@RequestBody User user){
        log.info("更新用户: {}", user);
        userService.update(user);
        return Result.success();
    }


    @GetMapping("/role/{roleLabel}")
    public Result getUsersByRoleLabel(@PathVariable("roleLabel") String roleLabel){
        log.info("查询角色: {}", roleLabel);
        List<User> users = userService.listByRole(roleLabel);
        return Result.success(users);
    }


    @GetMapping("/dept/{deptId}")
    public Result getUsersByDeptId(@PathVariable Integer deptId){
    	log.info("查询部门: {}", deptId);
    	List<User> users = userService.listByDeptId(deptId);
    	return Result.success(users);
    }


    @GetMapping("/list")
    public Result getAll(){
        log.info("查询所有");
        List<User> users = userService.listAll();
        return Result.success(users);
    }
}
